package com.info.zhiduoduo.repository.mapper.marketingcooperative;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.marketingcooperative.MarketingCooperativeBrokerageDto;
import com.info.zhiduoduo.repository.dto.marketingcooperative.MarketingCooperativeRecordDto;
import com.info.zhiduoduo.repository.entity.marketingcooperative.MarketingCooperativeRecord;

import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MarketingCooperativeRecordMapper extends BaseMapper<MarketingCooperativeRecord> {


	@Select(" select mcr.*,pa.realname as partnerName,c.compname " +
			" from zdd_marketing_cooperative_record mcr " +
			" join zdd_marketing_cooperative mc on mc.partnerid = mcr.partnerid and mc.state = 1 " +
			" join zdd_partner_account pa on pa.partnerid = mcr.partnerid " +
			" join zdd_company c on c.compid = mcr.compid " +
			" where mcr.partnerid = #{partnerid} " +
			" and if(#{state}=0, 1=1, mcr.state = #{state}) " +
			" and mcr.company_note like '%${companyNote}%' " +
			" order by id desc")
	List<MarketingCooperativeRecordDto> listByPartnerid(int partnerid, String contactName, String companyNote, int state);

	@Select(" select mcr.*,pa.realname as partnerName,c.compname " +
			" from zdd_marketing_cooperative_record mcr " +
			" join zdd_marketing_cooperative mc on mc.partnerid = mcr.partnerid and mc.state = 1 " +
			" join zdd_partner_account pa on pa.partnerid = mcr.partnerid " +
			" join zdd_company c on c.compid = mcr.compid " +
			" where mcr.compid = #{compid} " +
			" and if(#{state}=0, 1=1, mcr.state = #{state}) " +
			" and pa.realname like '%${partnerName}%' " +
			" and mcr.company_note like '%${companyNote}%' " +
			" order by id desc ")
	List<MarketingCooperativeRecordDto> listByCompid(int compid, String partnerName, String contactName, String companyNote, int state);

	@Select(" select count(*) as `count`, sum(brokerage) as totalBrokerage, sum(if(`state` = 2, brokerage, 0)) as undealedBrokerage," +
			" sum(if(`state` = 4, brokerage, 0)) as dealedBrokerage " +
			" from zdd_marketing_cooperative_record " +
			" where partnerid = #{partnerid} " +
			" and `state` in(2,4)")
	MarketingCooperativeBrokerageDto countBrokerage(int partnerid);
}
